package util;

/**
 * @Author czl
 * @Date 2023-10-09
 */
import java.sql.*;

public class DbUtil {
    private static final String driver="com.mysql.cj.jdbc.Driver";  //驱动  类名
    private static final String url = "jdbc:mysql://localhost:3306/jdbc_test"; // 修改为自己的数据库地址、端口号和名称
    private static final String username = "root"; // 修改为自己的数据库用户名
    private static final String password = "040311"; // 修改为自己的数据库密码




    static {
        /*
         * 加载驱动程序
         */
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            System.out.println("加载驱动程序失败...");
            e.printStackTrace();
        }
    }

    public static Connection getConnection() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            System.out.println("获取连接对象失败...");
            e.printStackTrace();
        }
        return conn;
    }

    public static void close(Connection conn, Statement stmt, ResultSet rst) {
        if (rst != null) {
            try {
                rst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }





    public static void main(String[] args) {
        Connection conn = DbUtil.getConnection();
        System.out.println(conn);
    }

}
